<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\Admin\GroupRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GroupController extends Controller
{
    protected $groupRepository;
    public function __construct(GroupRepository $groupRepository)
    {
        $this->groupRepository = $groupRepository;
    }
    /**
     * 改变分组的状态
     *
     */
    public function changeGroupStatus(Request $request)
    {
        if($request->has('group_update_data')){
            $groupData = $request->post('group_update_data');
            $group_id = $groupData['group_id'];
            $group_status = $groupData['group_status'];
            $result = $this->groupRepository->updateStatus($group_id,$group_status);
            if($request)
                return json_encode(['code'=>200,'msg'=>'修改成功']);
            else
                return json_encode(['code'=>400,'msg'=>'保存分组数据时出错']);
        }else{
            return json_encode(['code'=>400,'msg'=>'没有传入相应的分组数据']);
        }
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $groups = $this->groupRepository->getAll();
        if($groups)
            return json_encode(['code'=>200,'groups'=>$groups]);
        else
            return json_encode(['code'=>400,'msg'=>'获取分组信息出错']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if($request->has('group_data')){
            $groupData = $request->post('group_data');
            $group = [
                'group_thumbnail' => $groupData['group_thumbnail'],
                'group_name' => $groupData['group_name'],
                'group_status' => $groupData['group_status'],
            ];
            $result = $this->groupRepository->createGroup($group);
            if($request)
                return json_encode(['code'=>200,'group_id'=>$result->group_id]);
            else
                return json_encode(['code'=>400,'msg'=>'保存分组数据时出错']);
        }else{
            return json_encode(['code'=>400,'msg'=>'没有传入相应的分组数据']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
